<template>
  <div class="login-container">
    <div class="login-box">
      <div class="avatar_box">
        <img src="@/assets/logo.png" alt="">
      </div>
      <div class="user-form">
        <el-form ref="loginFormRef" :model="userinfo" :rules="rules">
          <el-form-item prop="username">
            <el-input prefix-icon="iconfont icon-user" placeholder="请输入用户名" v-model="userinfo.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="iconfont icon-3702mima" placeholder="请输入密码" show-password v-model='userinfo.password'></el-input>
          </el-form-item>
          <el-form-item class="button">
            <el-button type="primary" @click="loginFrom">登录</el-button>
            <el-button @click="resetFrom" type="info">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>
// 导入封装好的登录api接口
import { postLoginAPI } from '@/api/loginAPI.js'

export default {
  name: 'login',
  data() {
    return {
      userinfo: {
        username: '',
        password: ''
      },
      // 表单验证规则
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置表单
    resetFrom() {
      this.$refs.loginFormRef.resetFields()
    },
    // 登录
    loginFrom() {
      // 登录前进行验证返回值是bool类型
      this.$refs.loginFormRef.validate(async valid => {
        console.log(valid)
        if (!valid) {
          return false
        } else {
          // 验证通过就发起登录请求
          const { data: res } = await postLoginAPI(this.userinfo.username, this.userinfo.password)
          // 失败
          if (res.meta.status !== 200) {
            this.$message.error('用户名或密码错误！')
          } else {
            // 成功
            console.log(res)
            // 本地存储token
            localStorage.setItem('token', res.data.token)
            // 弹出提示框
            this.$message.success('登录成功')
            // 路由跳转
            this.$router.push('/home')
          }
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  background-color: #2b4b6b;
  height: 100%;
  .login-box {
    height: 300px;
    width: 450px;
    background-color: white;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .avatar_box {
      overflow: hidden;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      padding: 10px;
      height: 140px;
      width: 140px;
      border: 1px solid #ccc;
      border-radius: 50%;
      box-shadow: 0 0 10px #eee;
      img {
        width: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .user-form {
      width: 100%;
      position: absolute;
      padding: 0 40px;
      bottom: 20px;
      .button {
        margin-left: 110px;
      }
    }
  }
}
</style>
